/**
 * 应收应付报表管理初始化  明细
 */
var BorrowingInfo = {
    id: "BorrowingInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
BorrowingInfo.initColumn = function () {
return [
        [
		    {field: 'selectItem', radio: true,colspan: 1,rowspan: 2,width:'36px'},
            {title: '主键id', field: 'id', visible: false, align: 'center', valign: 'middle',switchable:false,colspan: 1,rowspan: 2,},
			{title: '报表统计日期', field: 'revDate', visible: true, align: 'center', valign: 'middle',
			formatter:function(value,row,index){
                        return value.substring(0,10);
				}
			,colspan: 1,rowspan: 2},
            {title: '摘要', field: 'note', visible: true, align: 'center', valign: 'middle',colspan: 1,rowspan: 2,width:'15%'},
            {title: '往来客户', field: 'contactName', visible: true, align: 'center', valign: 'middle',colspan: 1,rowspan: 2},
			{title: '应收',valign:"middle",align:"center",colspan: 3,rowspan: 1},
            {title: '应付',valign:"middle",align:"center",colspan: 3,rowspan: 1},
            {title: '应收应付余额', field: 'balance', visible: true, halign:'center', align: 'right', valign: 'middle',colspan: 1,rowspan: 2},

		],
	[
	        {title: '披索金额', field: 'pisoAmount', visible: true, halign:'center', align: 'right', valign: 'middle',
			formatter:function(value,row,index){ if(row.type==1){return value;}else{ return '';}}},
            {title: 'piso折人民币', field: 'pisoToRmb', visible: true, halign:'center', align: 'right', valign: 'middle',
			formatter:function(value,row,index){ if(row.type==1){return value;}else{ return '';}}},
            {title: '人民币金额', field: 'rmbAmount', visible: true, halign:'center', align: 'right', valign: 'middle',
            formatter:function(value,row,index){ if(row.type==1){return value;}else{ return '';}}},
			{title: '披索金额', field: 'pisoAmount', visible: true, halign:'center', align: 'right', valign: 'middle',
			formatter:function(value,row,index){ if(row.type==2){return value;}else{ return '';}}},
            {title: 'piso折人民币', field: 'pisoToRmb', visible: true, halign:'center', align: 'right', valign: 'middle',
			formatter:function(value,row,index){ if(row.type==2){return value;}else{ return '';}}},
            {title: '人民币金额', field: 'rmbAmount', visible: true, halign:'center', align: 'right', valign: 'middle',
            formatter:function(value,row,index){ if(row.type==2){return value;}else{ return '';}}},
	]

 ];
};

/**
 * 检查是否选中
 */
BorrowingInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        BorrowingInfo.seItem = selected[0];
        return true;
    }
};

BorrowingInfo.checkPayTime = function(){
    var beginDate =  $("#beginDate").val();
    var endDate =  $("#endDate").val();
    debugger;
    if (beginDate==null||beginDate==""){
        Feng.info("请选择要导出的开始时间");
        return false;
    } else {
        return true;
    }
    if (endDate==null||endDate==""){
        Feng.info("请选择要导出的结束时间");
        return false;
    } else {
        return true;
    }
};

/**
 * 点击添加应收应付报表
 */
BorrowingInfo.openAddBorrowingInfo = function () {
    var index = layer.open({
        type: 2,
        title: '添加应收应付报表',
        area: ['1000px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/borrowingInfo/borrowingInfo_add'
    });
    this.layerIndex = index;
};


/**
 * 添加期初余额
 */
BorrowingInfo.openAddBalance = function () {
    var index = layer.open({
        type: 2,
        title: '添加期初余额',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/borrowingInfo/borrowingInfo_addBalance'
    });
    this.layerIndex = index;
};


/**
 * 打开查看应收应付报表详情
 */
BorrowingInfo.openBorrowingInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '应收应付报表详情',
            area: ['1000px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/borrowingInfo/borrowingInfo_update/' + BorrowingInfo.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 导出
 */
BorrowingInfo.exportBorrowingInfo = function () {
    if (this.checkPayTime()) {
        window.location.href = "/borrowingInfo/load?beginDate=" + $("#beginDate").val()+"&endDate=" + $("#endDate").val();
    }


};

/**
 * 删除应收应付报表
 */
BorrowingInfo.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/borrowingInfo/delete", function (data) {
            Feng.success("删除成功!");
            BorrowingInfo.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("borrowingInfoId",this.seItem.id);
        ajax.start();
    }
};


BorrowingInfo.resetSearch = function () {
    $("#beginDate").val("");
    $("#endDate").val("");
	$("#type").val("");
}


/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
BorrowingInfo.formParams = function() {
    var queryData = {};
    queryData['beginDate'] = $("#beginDate").val();
    queryData['endDate'] = $("#endDate").val();
	queryData['type'] = $("#type").val();
	queryData['contact'] = $("#contact").val();
	queryData['platform'] = $("#platform").val();
    return queryData;
}

/**
 * 查询应收应付报表列表
 */
BorrowingInfo.search = function () {
    BorrowingInfo.table.refresh({query: BorrowingInfo.formParams()});
	$("#BorrowingInfoSum").remove();
    $("#BorrowingInfoTable").after(  BorrowingInfo.tatol()   )
};

/**
 * 合计
 */
BorrowingInfo.tatol= function(){
	
	var tatol;
    var ajax = new $ax(Feng.ctxPath + "/borrowingInfo/tatol", function (result) {
        tatol=result;
    }, function (result) {
        Feng.error("查询失败!" + result.responseJSON.message + "!");
    });
    ajax.setData(BorrowingInfo.formParams());
    ajax.start();
    var rowTem = '<div><table id="BorrowingInfoSum" data-mobile-responsive="true" data-click-to-select="false" class="table table-hover table-striped" style="margin-top: -0px;color: #2A2E36;font-size: 14px;"><tbody id="CashExpenditureSum" >'+
        '<tr data-index="0">'+
      '<td class="bs-checkbox " style="width: 35px;"></td> '+
		'<td style="text-align: center; vertical-align: middle;  "></td>'+
        '<td style="text-align: center; vertical-align: middle; width: 160% "></td>'+
		'<td style="text-align: center; vertical-align: middle; ">总计 </td> '+
		'<td style="text-align: right; vertical-align: middle; ">'+tatol.incomePiso+'</td>'+
		'<td style="text-align: right; vertical-align: middle; ">'+tatol.incomePTR+'</td>'+
		'<td style="text-align: right; vertical-align: middle; ">'+tatol.incomeRMB+'</td>'+
		'<td style="text-align: right; vertical-align: middle; ">'+tatol.outPiso+'</td>'+
		'<td style="text-align: right; vertical-align: middle; ">'+tatol.outPTR+'</td>'+
		'<td style="text-align: right; vertical-align: middle; ">'+tatol.outRMB+'</td>'+
		'<td style="text-align: right; vertical-align: middle; ">' +tatol.amount+'</td>'+
        '</tr></tbody></table></div>';
		
		

	
    return rowTem;
}

$(function () {
    var defaultColunms = BorrowingInfo.initColumn();
    var table = new BSTable(BorrowingInfo.id, "/borrowingInfo/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(BorrowingInfo.formParams());
	table.setShowColumns(false);
	table.setShowRefresh(false);
    BorrowingInfo.table = table.init();
	//添加表头
	$(".fixed-table-toolbar").after("<div style='text-align: center;vertical-align: middle;font-size: 22px;color: #6979d7;font-weight: bold;'> 应付/应收款项明细余额表</div>");
	$("#BorrowingInfoTable").after(BorrowingInfo.tatol());
});
